package com.jfinal.server.undertow;

import java.util.function.Consumer;

public class UndertowConfigPostProcessor implements Consumer<UndertowConfig> {
    @Override
    public void accept(UndertowConfig config) {
        if (config.isDevMode()) {
            configThreadsForDevMode(config);
        }

        configClassPath(config);
    }

    protected void configClassPath(UndertowConfig config) {
        if (config.getClassPathDirs().isEmpty()) {
            for (String classPathDir : UndertowKit.getClassPathDirs()) {
                config.addClassPathDir(classPathDir);
            }
        }
    }

    protected void configThreadsForDevMode(UndertowConfig config) {
        final int cpuNum = Runtime.getRuntime().availableProcessors();
        final int ioThreads = Math.min(4, cpuNum);
        final int workerThreads = ioThreads * 2;

        config.setIoThreads(ioThreads);
        config.setWorkerThreads(workerThreads);
    }

}
